package com.base.library.permission;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;

import com.base.library.R;


/**
 * 作为dialog fragment的基类
 */
public class PermissionTipDialogFragment extends DialogFragment {
    public AppCompatActivity mContext;
    private TextView mTitle;
    private TextView mContent;
    private boolean mIsHide;
    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mContext = (AppCompatActivity) context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_permission_tip_layout, container, false);
        setDialogAttrs();
        mTitle = view.findViewById(R.id.txtTitle);
        mContent = view.findViewById(R.id.txtTipContent);
        return view;
    }

    private void setDialogAttrs() {
        try {
            getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setDialogParams();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    public void initData() {
        Bundle bundle = getArguments();
        String title = bundle.getString("title");
        String content = bundle.getString("content");
        if (mTitle != null){
            mTitle.setText(title);
        }
        if (mContent != null){
            mContent.setText(content);
        }
    }
    /**
     * dialog宽度占满，高度自定义
     */
    public void setDialogParams() {
        try {
            Window dialogWindow = getDialog().getWindow();
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.dimAmount = 0.6f;
            lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
            lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            lp.gravity = Gravity.TOP;
            dialogWindow.setAttributes(lp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        mIsHide = true;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mIsHide){
            dismiss();
        }
    }
}
